package com.kxmall.web.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kxmall.common.exception.ServiceException;
import com.kxmall.huishou.domain.HsSettlementOrder;
import com.kxmall.huishou.domain.HsWasteCount;
import com.kxmall.huishou.mapper.HsSettlementOrderMapper;
import com.kxmall.huishou.mapper.HsWasteCountMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Component
@RequiredArgsConstructor
@Slf4j
public class huishouTask {

    private final HsSettlementOrderMapper hsSettlementOrderMapper;   //回收结算订单

    private final HsWasteCountMapper hsWasteCountMapper;   //废品回收统计

    @Scheduled(cron = "0 40 22 * * ?") // 每天凌晨2点
//    @Scheduled(cron = "0/10 * * * * ?") // 每天凌晨2点
    public void executeTask() {
        log.info("定时任务执行中,开始更新废品回收统计数据");

        // 查询 本月回收重量 废品回收总重量 废品回收总价值
        BigDecimal wasteTotalMonth = new BigDecimal(0);
        BigDecimal wasteTotal = new BigDecimal(0);
        BigDecimal wasteAmount = new BigDecimal(0);
        // 当前时间 本月一号
        LocalDateTime now = LocalDateTime.now();
        LocalDate firstDayOfMonth  = now.toLocalDate().withDayOfMonth(1);
        // 统计 今日回收重量 历史回收重量
        LambdaQueryWrapper<HsSettlementOrder> q = new LambdaQueryWrapper<>();
        q.eq(HsSettlementOrder::getStatus,4);
        List<HsSettlementOrder> hsSettlementOrders = hsSettlementOrderMapper.selectList(q);
        for (HsSettlementOrder hsSettlementOrder : hsSettlementOrders) {
            LocalDate localDate = hsSettlementOrder.getCreateTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            if(hsSettlementOrder.getWeightTal() != null){
                if (localDate.compareTo(firstDayOfMonth) >= 0) {
                    wasteTotalMonth = wasteTotalMonth.add(hsSettlementOrder.getWeightTal());
                }
                wasteTotal = wasteTotal.add(hsSettlementOrder.getWeightTal());
            }
            if(hsSettlementOrder.getAmountTal() != null) {
                wasteAmount = wasteAmount.add(hsSettlementOrder.getAmountTal());
            }
        }
        // 更新 废品回收统计
        HsWasteCount hsWasteCount = hsWasteCountMapper.selectById(1);
        if (hsWasteCount == null) {
            throw new ServiceException("回收统计信息未配置");
        }
//        BigDecimal divideDecimal = new BigDecimal(1000);
//        hsWasteCount.setWasteTotal(wasteTotal.divide(divideDecimal, 2, RoundingMode.HALF_UP));
//        hsWasteCount.setWasteTotalMonth(wasteTotalMonth.divide(divideDecimal, 2, RoundingMode.HALF_UP));
//        hsWasteCount.setWasteAmount(wasteAmount.divide(divideDecimal, 2, RoundingMode.HALF_UP));
        hsWasteCount.setWasteTotal(wasteTotal);
        hsWasteCount.setWasteTotalMonth(wasteTotalMonth);
        hsWasteCount.setWasteAmount(wasteAmount);

        hsWasteCountMapper.updateById(hsWasteCount);
    }


}
